package com.itheima.tlias.controller;

import com.itheima.tlias.annotation.LogOperation;
import com.itheima.tlias.pojo.*;
import com.itheima.tlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 *
 * @Author mr.luo
 * @Date 06 4月 2025 15:48
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 分页查询所有员工与部门信息
     */
//    @GetMapping
//    public Result pageQuery(String name,
//                            Integer gender,
//                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
//                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
//                            @RequestParam(defaultValue = "1") Integer page,
//                            @RequestParam(defaultValue = "10") Integer pageSize
//    ) {
//        log.info("分页查询所有员工信息，参数：page={},pageSize={}", page, pageSize);
//        PageResult<Emp> pageResult = empService.pageQuery(name, gender, begin, end,page, pageSize);
//        return Result.success(pageResult);
//    }

    /**
     * 优化1
     *
     * @param empQueryDTO
     * @return
     */
    @GetMapping
    public Result pageQuery(EmpQueryDTO empQueryDTO) {
        log.info("分页查询所有员工信息，参数：{}", empQueryDTO);
        PageResult<Emp> pageResult = empService.pageQuery(empQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 新增员工
     */
    @LogOperation
    @PostMapping
    public Result addEmp(@RequestBody Emp emp)  {
        log.info("新增员工，员工信息：{}", emp);
        empService.addEmp(emp);
        return Result.success();
    }

    /**
     * 查询全部员工
     */
    @GetMapping("/list")
    public Result list() {
        log.info("查询全部员工");
     List<Emp> empList = empService.selectList();
        return Result.success(empList);
    }

    /**
     * 批量删除员工信息
     */
    @LogOperation
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids)  {
        log.info("删除员工，员工id：{}", ids);
        empService.deleteById(ids);
        return Result.success();
    }

    /**
     * 根据id查询员工信息
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer uid) {
        log.info("根据id查询员工信息，员工id：{}", uid);
        Emp emp = empService.findById(uid);
        return Result.success(emp);
    }

    /**
     * 修改员工信息
     */
    @LogOperation
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工信息：{}", emp);
        empService.update(emp);
        return Result.success();
    }
    /**
     * 忘记密码
     */
    @PutMapping("/forgetPwd")
    public Result forgetPwd(@RequestBody ForgetPwdDTO forgetPwdDTO) {
        log.info("忘记密码，员工信息：{}", forgetPwdDTO);
        empService.forgetPwd(forgetPwdDTO);
        return Result.success();
    }

    /**
     * 修改密码
     */
    @PutMapping("/updatePwd")
    public Result updatePwd(@RequestBody UpdatePwdDTO updatePwdDTO) {
        log.info("修改密码，员工信息：{}", updatePwdDTO);
        empService.updatePwd(updatePwdDTO);
        return Result.success();
    }
    /**
     * 根据员工userName查询员工信息
     */
    @GetMapping("/name/{userName}")
    public Result getUserDetial(@PathVariable("userName") String userName) {
        log.info("根据员工userName查询员工信息，员工userName：{}", userName);
        Emp emp = empService.getUserDetial(userName);
        return Result.success(emp);
    }
}
